package text.adventure.input;

import java.util.HashMap;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SynHandler extends DefaultHandler {

	StringBuffer buff = null;
    boolean buffering = false; 
    
    Map<String, String> synMap = new HashMap<String, String>();
    String current = "";
    
    
    public Map<String, String> getResultMap()
    {
    	return synMap;
    }
    
    @Override
    public void startElement(String namespaceURI, String localName, String qName, 
            Attributes atts) throws SAXException {
        if (localName.equals("synonymgroup")) {
        	current = atts.getValue("id");
        } else if (localName.equals("synonym")) {
            buff = new StringBuffer("");
            buffering = true;
        }
    } 
    
    @Override
    public void characters(char ch[], int start, int length) {
        if(buffering) {
            buff.append(ch, start, length);
        }
    } 
    
    @Override
    public void endElement(String namespaceURI, String localName, String qName) 
    throws SAXException {
    	if (localName.equals("synonym")) {
            buffering = false; 
            String content = buff.toString();
            synMap.put(content, current);
        }
    }
}
